package main

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

// User 结构体声明
type User struct {
	UserId    int64  `gorm:"primaryKey;autoIncrement"`
	UserName  string `gorm:"not null;type:varchar(32)"`
	UserPwd   string `gorm:"not null;type:varchar(128)"`
	UserPhone string `gorm:"unique;type:varchar(32)"`
}

func (*User) TableName() string {
	return "t_user"
}

func main() {
	// 连接数据库
	var dsn = "root:QWEzxc123456@tcp(127.0.0.1:3306)/gorm_test"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	// 查询user_id为1的记录
	var stu User
	db.Where("user_id = ?", 1).Find(&stu)
	// 修改stu姓名为wjj1
	stu.UserName = "wjj1"
	// 修改(按照主键修改)
	db.Save(&stu)
}
